home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1994 December / PSL Monthly Shareware CD-ROM (Public Software Library)(December 1994).bin / prgmming / dos / c2 / winmax.h < prev    next >
Text File  |  1991-06-10  |  4KB  |  74 lines

  1. // WINMAX.H - max() and min() functions for C++ DOS and Windows programs.
  2. // Uploaded by Dan O'Connor (71161,1517) for the benefit of everyone!
  3.  
  4. //     Those who have tried to compile the examples from Charles Petzold's
  5. //  "Programming Windows, 2ed." using C++ have discovered that the max()
  6. //  and min() macros are excluded when compiling under C++.  This is done
  7. //  on purpose to allow C++ classes to define their own max() and min()
  8. //  member functions.  The presence of the macros defeates C++'s ability
  9. //  to overload these functions, since the preprocessor gets to them first!
  10. //
  11. //     The answer, in accordance with ARM Sec. 16.1c, is to use overloaded
  12. //  inline functions.  Inline functions will produce the same code as the
  13. //  macro,  but the use of inline functions has two additional benefits:
  14. //  (1) the linker can enforce type-safe linkage; this is not possible with
  15. //  preprocessor macros; and (2) inline functions allow overloading, thus max()
  16. //  and min() will not collide with classes that also define max() and min().
  17. //
  18. //     This header defines four inline functions: max(), min(), absMax(), and
  19. //   absMin().  The purpose of each is described below:
  20. //
  21. //   max()      Returns the argument with the larger value.
  22. //   min()      Returns the argument with the smaller value.
  23. //   absMax()   Returns the argument with the larger absolute value (magnitude).
  24. //   absMin()   Returns the argument with the smaller absolute value.
  25. //
  26. //     Each function is overloaded for four data types, int, long int, float,
  27. //   and double.  If there are other data types you need, such as char, unsigned
  28. //   int, etc., feel free to add it!  Overloading is good for the soul!  Keep in
  29. //   mind that absMax() and absMin() need not be defined for unsigned types,
  30. //   since unsigned types are, by definition, positive numbers.
  31. //
  32. //     It is my hope that these functions will help eliminate the confusion
  33. //   inherent with trying to use the max() and min() macros under C++.
  34. //
  35. //   Good luck to all,
  36. //     Dan O'Connor
  37. //     71161,1517
  38.  
  39.  
  40. #ifndef WINMAX_H       // Only include once!
  41. #define WINMAX_H       // Define name on first pass thru!
  42.  
  43. #ifndef __cplusplus
  44. #error WINMAX Requires C++ to Compile        // Oops! Overloading requires C++.
  45. #endif
  46.  
  47. #include <math.h>      // Needed for absolute value functions.
  48.  
  49. // --- max() functions ---
  50. inline int    max(int x, int y)  { return ( x > y ? x : y ); }
  51. inline long   max(long x, long y)  { return ( x > y ? x : y ); }
  52. inline int    max(float x, float y)  { return ( x > y ? x : y ); }
  53. inline double max(double x, double y)  { return ( x > y ? x : y ); }
  54.  
  55. // --- min() functions ---
  56. inline int    min(int x, int y)  { return ( x < y ? x : y ); }
  57. inline long   min(long x, long y)  { return ( x < y ? x : y ); }
  58. inline float  min(float x, float y)  { return ( x < y ? x : y ); }
  59. inline double min(double x, double y)  { return ( x < y ? x : y ); }
  60.  
  61. // --- absMax() functions ---
  62. inline int    absMax(int x, int y) { return ( abs(x) > abs(y) ? x : y ); }
  63. inline long   absMax(long x, long y) { return ( labs(x) > labs(y) ? x : y ); }
  64. inline float  absMax(float x, float y) { return (fabs(x) > fabs(y) ? x : y); }
  65. inline double absMax(double x, double y) { return (fabs(x) > fabs(y) ? x : y); }
  66.  
  67. // --- absMin() functions ---
  68. inline int    absMin(int x, int y) { return (abs(x) < abs(y) ? x : y); }
  69. inline long   absMin(long x, long y) { return (labs(x) < labs(y) ? x : y); }
  70. inline float  absMin(float x, float y) { return (fabs(x) < fabs(y) ? x : y); }
  71. inline double absMin(double x, double y) { return (fabs(x) < fabs(y) ? x : y); }
  72.  
  73. #endif
  74.